﻿using Intex.Controls.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Intex.Controls.Stopwatch
{
    /// <summary>
    /// StopwatchControlConfigDialog.xaml 的交互逻辑
    /// </summary>
    public partial class StopwatchControlConfigDialog : Window
    {
        private StopwatchControl control = null;
        private StopwatchConfig config = null;

        public StopwatchControlConfigDialog(StopwatchControl control)
        {
            InitializeComponent();

            this.control = control;

            InitializeCombobox();

            this.config = this.control.LoadConfig();

            this.DataContext = this.config;
        }

        private void InitializeCombobox()
        {
            var portNames = SerialPort.GetPortNames();
            foreach (var item in portNames)
            {
                this.cbxPortName.Items.Add(item);
            }

            if (portNames.Length <= 0)
            {
                this.cbxPortName.Items.Add("COM1");
            }

            //奇偶较验
            this.cbxParity.Items.Add(Parity.None);
            this.cbxParity.Items.Add(Parity.Odd);
            this.cbxParity.Items.Add(Parity.Even);
            this.cbxParity.Items.Add(Parity.Mark);
            this.cbxParity.Items.Add(Parity.Space);

            //停止位
            this.cbxStopBit.Items.Add(StopBits.One);
            this.cbxStopBit.Items.Add(StopBits.OnePointFive);
            this.cbxStopBit.Items.Add(StopBits.Two);

            //流控制
            this.cbxHandshake.Items.Add(Handshake.None);
            this.cbxHandshake.Items.Add(Handshake.XOnXOff);
            this.cbxHandshake.Items.Add(Handshake.RequestToSend);
            this.cbxHandshake.Items.Add(Handshake.RequestToSendXOnXOff);

            //码表类型
            this.cbxStopwatchType.Items.Add(StopwatchType.None);
            this.cbxStopwatchType.Items.Add(StopwatchType.XM058B);

        }

        public static bool? ShowConfig(StopwatchControl control)
        {
            var dialog = new StopwatchControlConfigDialog(control);
            return dialog.ShowDialog();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            //检验

            if (string.IsNullOrWhiteSpace(txtBaudRate.Text))
            {
                MessageBox.Show("请输入比特率。", "出错了", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            int obj;
            if (!int.TryParse(this.txtBaudRate.Text, out obj))
            {
                MessageBox.Show("请输入正确的比特率。", "出错了", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (!int.TryParse(this.txtDataBits.Text, out obj))
            {
                MessageBox.Show("请输入正确的数据位。", "出错了", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else
            {
                if (obj < 1 || obj > 8)
                {
                    MessageBox.Show("请输入正确的数据位(1-8位)。", "出错了", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            var xml = XmlSerializerHelper.Serialize<StopwatchConfig>(this.config);
            File.WriteAllText(control.ConfigFileName, xml, Encoding.UTF8);

            this.DialogResult = true;
        }


        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
